package com.fly.control;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.fly.conn.DBConnect;
import com.fly.model.User;

public class UserControl {
	public static User loginCheck(String name){
		DBConnect dbc = new DBConnect();
		String sql = "select * from user where loginName ='" + name + "'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		User u = new User();
		try{
			rs = dbc.executeQuery();
			if(rs.next()){
				u.setId(rs.getInt("id"));
				u.setName(rs.getString("name"));
				u.setRole(rs.getInt("role"));
				u.setLoginName(name);
				u.setPassword(rs.getString("password"));
				return u;
			} else{
				return null;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("调用loginCheck函数失败");
			return null;
		} finally{
			dbc.close();
		}
	}
	public static boolean insertUser(User u){
		String sql = "insert into user(name, role, loginName, password) values(?, ?, ?, ?)";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			int i = 1;
			dbc.setString(i++, u.getName());
			dbc.setInt(i++, u.getRole());
			dbc.setString(i++, u.getLoginName());
			dbc.setString(i++, u.getPassword());
			if(dbc.executeUpdate()){
				return true;
			}
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("insertUser Error");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean deleteUser(int id){
		String sql = "delete * from user where id ="+ id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			if(dbc.executeUpdate())
				return true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("deleteUser ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static User getUser(int id){
		String sql = "Select * from user where id = " + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		User u = new User();
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				u.setId(id);
				u.setName(rs.getString("name"));
				u.setRole(rs.getInt("role"));
				u.setLoginName(rs.getString("loginName"));
				u.setPassword(rs.getString("password"));
			}
			return u;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("getUser ERROR");
		} finally{
			dbc.close();
		}
		return null;
	}
	public static List<User> getAllUsers(){
		String sql = "select * from user";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		List<User> list = new ArrayList<User>();
		try{
			rs = dbc.executeQuery();
			while (rs.next()) {
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setName(rs.getString("name"));
				u.setRole(rs.getInt("role"));
				u.setLoginName(rs.getString("loginName"));
				u.setPassword(rs.getString("password"));
				list.add(u);
			}
			return list;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("getAllUsers ERROR");
		} finally{
			dbc.close();
		}
		return null;
	}
}
